package cn.hg.jfsa.manage.controller.system.validator;

import com.jfinal.core.Controller;

import cn.hg.base.jfinal.validator.BaseValidator;
import cn.hg.base.kit.PasswordKit;
import cn.hg.jfsa.core.JfsaThreadLocal;
import cn.hg.jfsa.model.JfsaAdmin;

public class BaseInfoValidator extends BaseValidator {

	@Override
	protected void handleError (Controller controller) {
		controller.set("admin", controller.getBean(JfsaAdmin.class, "admin"));
	}

	@Override
	protected void validate (Controller controller) {

		if ("POST".equalsIgnoreCase(controller.getRequest().getMethod())) {
			// 更新基本资料时必须输入密码
			// validateRequired("oldPassword", "flashMsg", "必须输入旧密码！");
			this.validateString("oldPassword", 6, 20, "flashMsg", "旧密码必须填写6到20长度的字符!");
			// 验证密码
			JfsaAdmin admin = JfsaThreadLocal.getAdmin();

			if (!PasswordKit.validatePassword(admin.getSalt(), controller.getPara("oldPassword"),
				admin.getPassword())) {
				this.addError("flashMsg", "当前密码不正确！");
			}

			// 新密码验证
			if (!controller.isParaBlank("newPassword")) {
				// this.validateRequired("newPassword", "flashMsg", "必须输入新密码！");
				this.validateString("newPassword", 6, 20, "flashMsg", "新密码必须填写6到20长度的字符!");
				this.validateEqualField("newPassword", "confirmPassword", "flashMsg", "确认密码必须和新密码相同！");
			}

			// 昵称验证
			// this.validateRequired("admin.name", "flashMsg", "必须输入昵称！");
			this.validateString("admin.name", 2, 20, "flashMsg", "昵称必须填写2到20长度的字符!");

			if (!controller.isParaBlank("admin.email")) {
				this.validateEmail("admin.email", "emailMsg", "电子邮件格式不正确！");
			}

			if (!controller.isParaBlank("admin.mobilePhone")) {
				this.validateMobilePhone("admin.mobilePhone", "mobilePhoneMsg", "手机号码格式不正确！");
			}

			if (!controller.isParaBlank("admin.telephone")) {
				this.validateTelePhone("admin.telephone", "telephoneMsg", "电话号码格式不正确！");
			}

		}

	}

}
